package cn.edu.henu.team.ui_test;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.base.BaseActivity;
import cn.edu.henu.team.bean._User;
import cn.edu.henu.team.bean.upLog;

public class FindLogActivity extends BaseActivity {
    private String objectId;
    private ImageView back;
    private TextView time;
    private TextView location;
    private TextView administration;
    private TextView content;
    private TextView answer;
    private ArrayList<String> picList = new ArrayList<>();
    private Boolean isHaven;  // 判断是否存在图片
    private NineGridView nineGridView;

    @Override
    protected int initLayout() {
        return R.layout.activity_find_log;
    }

    @Override
    protected void initView() {
        objectId = getIntent().getStringExtra("objectId");
        Log.d("1522**********",objectId);
        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        time = findViewById(R.id.time);
        location = findViewById(R.id.location);
        administration = findViewById(R.id.administration);
        content = findViewById(R.id.content);
        answer = findViewById(R.id.answer);
        nineGridView = findViewById(R.id.comm_nine);
    }

    @Override
    protected void initData() {
        BmobQuery<upLog> query = new BmobQuery<upLog>();

        query.getObject(objectId, new QueryListener<upLog>() {
            @Override
            public void done(upLog upLog, BmobException e) {
                if (e == null) {
                   time.setText(upLog.getTime());
                   location.setText(upLog.getLocation());
                   administration.setText(upLog.getAdministration());
                   content.setText(upLog.getContent());
                   answer.setText(upLog.getAnswer());
                   initPics(upLog.getHeadImgUrl());
//                   Log.d("+++++++++",upLog.getHeadImgUrl().get(0));
                } else {
                    Toast.makeText(getApplicationContext(), "记录查询失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /*
   加载帖子图片集合
    */
    public void initPics(List<String> picList) {

        if (picList.size() > 0) {//判断是否有图片
            ArrayList<ImageInfo> imageInfo = new ArrayList<>();
            for (int j = 0; j < picList.size(); j++) {
                ImageInfo info = new ImageInfo();
                info.setThumbnailUrl(picList.get(j));
                info.setBigImageUrl(picList.get(j));
                imageInfo.add(info);
            }
            nineGridView.setAdapter(new NineGridViewClickAdapter(FindLogActivity.this, imageInfo));
        } else {
            nineGridView.setVisibility(View.GONE);
        }
    }
}